<?php 
/*
   Copyright 2010, Sanket Parab

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   
   This is part of the e-TAT Architecture Package and will be featured in the final e-TAT Application (Phase 1).
   
   Instructor class: Holds data on the instructor currently signed-in into eTAT
 */
class Instructor {
	/**
	 * Constructor
	 * @param $id ID of the Instructor
	 */
	function __construct($id) {
		$query = mysql_query("SELECT * FROM `instructor` WHERE `id` = '$id';") or die(mysql_error());
		$result = mysql_fetch_object($query);
		if($result) {
			foreach($result as $key=>$value) {
				$this->$key = $value;
			}
		}
	}
	/**
	 * Update instructor data
	 */
	public function updateInstructor() {
		$query = mysql_query("UPDATE `instructor` SET `full_name` = '$this->full_name', `email` = '$this->email', `permission` = '$this->permissiom' WHERE `id` = '$this->id'") or die(mysql_error());
		return true;
	}
	/**
	 * Get all courses created by this instructor
	 */
	public function getAllCourses() {
		$courses = null;
		$query = mysql_query("SELECT * FROM `course` WHERE `owner_id` = '$this->id'") or die(mysql_error());
		while($r = mysql_fetch_object($query)) {
			$courses[] = new Course($r->id);
		}
		return $courses;
	}
	/**
	 * Get a single course created by this instructor
	 * @param $id requires the course id
	 */
	public function getCourse($id) {
		$courses = $this->getAllCourses();
		foreach($courses as $c) { // Groups can be fetched from within a Course. So, first fetch all Courses.
			if($id == $c->id)
			$course = $c;
		}
		if($course) {
			return $course;
		} else {
			return false;
		}
	}
	/**
	 * Get a single group belonging to one of the courses created by this instructor
	 * @param $id requires the group id
	 */
	public function getGroup($id) {
		$courses = $this->getAllCourses();
		if($courses) {
			foreach($courses as $c) {
				$groups = $c->getAllGroups();
				if($groups) {
					foreach($groups as $g) {
						if($g->id == $id) {
							return new Group($id);
						}
					}
				} else {
					return false;
				}
			}
		} else {
			return false;
		}
	}
	/**
	 * Adds a new course
	 * @param $data OBJECT ARRAY
	 * $data->title course title
	 * $data->term course term
	 * $data->description course description
	 */
	public function addCourse($data) {
		$query = mysql_query("INSERT INTO `course` VALUES (NULL, $this->id , '$data->title', '$data->term', '$data->description')") or die(mysql_error());
		$all = $this->getAllCourses();
		foreach($all as $a) {
			if($a->term == $data->term) {
				$course = $a;
			}
		}
		return $course->id;
	}
	/**
	 * Removes one of the courses created by this instructor
	 */
	public function removeCourse($id) {
		$all = $this->getAllCourses();
		foreach($all as $a) {
			if($a->id == $id) {
				$a->owner_id = '';
				$a->updateCourse();
				break;
			}
		}
		return true;
	}
	/**
	 * Shares a course with another Instructor
	 */
	public function shareCourse($course_id,$instructor_id) {
		$share = new Instructor($instructor_id);
		$course = getCourse($course_id);
		if($course && $share) {
			$course->addOwner($share);
			return true;
		} else {
			return false;
		}
	}
	/**
	 * Adds an Annotation to a specific Course
	 * @param $course_id ID of the Course
	 * @param $comment message of the Annotation
	 */
	public function addCourseAnnotation($course_id,$comment) {
		$date = time();
		mysql_query("INSERT INTO `annotation` VALUES (NULL,'$date','$this->id','$course_id',NULL,NULL,'$comment');") or die(mysql_error());
		return true;
	}
	/**
	 * Returns an array of all Course Annotations made by this Instructor. 
	 *
	 */
	public function getAllCourseAnnotations() {
		$query = mysql_query("SELECT * FROM `annotation` WHERE `instructor_id` = '$this->id' AND `course_id` IS NOT NULL;");
		while($r = mysql_fetch_object($query)) {
			$annotations[] = new Annotation($r->id);
		}
		return $annotations;
	}
	/**
	 * Adds an Annotation to a specific Group
	 * @param $group_id ID of the Group
	 * @param $comment message of the Annotation
	 */
	public function addGroupAnnotation($group_id,$comment) {
		$date = time();
		mysql_query("INSERT INTO `annotation` VALUES (NULL,'$date','$this->id',NULL,'$group_id',NULL,'$comment');") or die(mysql_error());
		return true;
	}
	/**
	 * Returns an array of all Group Annotations made by this Instructor. 
	 *
	 */
	public function getAllGroupAnnotations() {
		$query = mysql_query("SELECT * FROM `annotation` WHERE `instructor_id` = '$this->id' AND `group_id` IS NOT NULL;");
		while($r = mysql_fetch_object($query)) {
			$annotations[] = new Annotation($r->id);
		}
		return $annotations;
	}
	/**
	 * Adds an Annotation to a specific Student
	 * @param $student_id ID of the Student
	 * @param $comment message of the Annotation
	 */
	public function addStudentAnnotation($student_id,$comment) {
		$date = time();
		mysql_query("INSERT INTO `annotation` VALUES (NULL,'$date','$this->id',NULL,NULL,'$student_id','$comment');") or die(mysql_error());
		return true;
	}
	/**
	 * Returns an array of all Course Annotations made by this Instructor. 
	 *
	 */
	public function getAllStudentAnnotations() {
		$query = mysql_query("SELECT * FROM `annotation` WHERE `instructor_id` = '$this->id' AND `student_id` IS NOT NULL;");
		while($r = mysql_fetch_object($query)) {
			$annotations[] = new Annotation($r->id);
		}
		return $annotations;
	}
	/**
	 * Returns a specific Annotation made by this Instructor.
	 * @param $annotation_id Requires the annotation ID.
	 *
	 */
	public function getAnnotation($annotation_id) {
		return new Annotation($annotation_id);
	}
	/**
	 * Removes a specific Annotation created by this Instructor permanently.
	 * @param $annotation_id Requires Annotation ID.
	 */
	public function deleteAnnotation($annotation_id) {
		mysql_query("UPDATE `annotation` SET `instructor_id` = NULL  WHERE `instructor_id` = '$this->id' AND `id` = '$annotation_id'") or die(mysql_error());
		return true;
	}
	/**
	 * Returns all the surveys created by this instructor
	 */
	public function getAllSurveys(){
		$query = mysql_query("SELECT * FROM `survey` WHERE `instructor_id` = '$this->id'") or die (mysql_error());
		while($r = mysql_fetch_object($query)) {
			$surveys[] = new Survey($r->id);
		}
		if($surveys) {
			return $surveys;
		} else {
			return false;
		}
	}
	/**
	 * Returns a single survey created by this instructor
	 * @param $id requires the survey id
	 */
	public function getSurvey($id) {
		$query = mysql_query("SELECT * FROM `survey` WHERE `instructor_id` = '$this->id' AND `id` = '$id';") or die(mysql_error());
		$result = mysql_fetch_object($query);
		if($result) {
			return new Survey($result->id);
		} else {
			return false;
		}
	}
	/**
	 * Creates a new survey for this instructor
	 * @param $data survey data string required
	 */
	public function createSurvey($data) {
		$sql = mysql_query("INSERT INTO `survey` VALUES (
			NULL,
			'$this->id',
			'$data->survey_name',
			'$data->survey_title',
			'$data->welcome_message',
			'$data->confirmation_message',
			'$data->email_subject',
			'$data->email_message',
			'$data->date_created',
			'$data->last_modified',
			NULL,
			NULL,
			NULL
		)") or die(mysql_error());
		return true;
	}
	/**
	 * Deletes a survey created by this instructor
	 * @param $id requires survey id
	 */
	public function deleteSurvey($id) {
		mysql_query("DELETE FROM `survey` WHERE `id` = '$id'") or die(mysql_error());
	    mysql_query("DELETE FROM `question` WHERE `survey_id` = '$id'") or die(mysql_error());
		mysql_query("DELETE FROM `response` WHERE `survey_id` = '$id'") or die(mysql_error());
	    mysql_query("DELETE FROM `response_value` WHERE `survey_id` = '$id'") or die(mysql_error());
	}
	/**
	 * Copies a survey created by this instructor
	 * @param $id requires the id of the survey to be copied
	 */
	public function copySurvey($id) {
		$s = $this->getSurvey($id);
		$s->survey_name .= '_copy';
		$query = mysql_query("INSERT INTO `survey` VALUES
			(NULL,
			'$this->id',
			'$s->survey_name',
			'$s->survey_title',
			'$s->welcome_message',
			'$s->confirmation_message',
			'$s->email_subject',
			'$s->email_message',
			'$s->date_created',
			'$s->last_modified',
			'$s->schedule',
			'$s->last_sent',
			'$s->scheduled_to_groups');
		") or die(mysql_error());
		$query = mysql_query("SELECT MAX(`id`) FROM `survey` WHERE `instructor_id`='$this->id'") or die(mysql_error());
		$result = mysql_fetch_row($query);
		$new = $this->getSurvey($result[0]);
		if($new) {
			$questions = $s->getAllQuestions();
			if($questions) {
				foreach($questions as $q) {
					$data = (object) array(
		              'type'=>$q->type,
		              'required'=>$q->required,
		              'question'=>$q->question,
		              'create_section'=>$q->create_section,
		              'order'=>$q->order,
		              'response1'=>$q->response1,
					  'response2'=>$q->response2,
					  'response3'=>$q->response3,
					  'response4'=>$q->response4,
					  'response5'=>$q->response5,
					  'response6'=>$q->response6
					);
					$new->addQuestion($data);
				}
			}
		}
	}
}